package com.tokenmaster.aitrade.market.virtual;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import com.tokenmaster.aitrade.common.ticker.TickerData;
import com.tokenmaster.aitrade.common.ticker.Ticker_Spot;
import com.tokenmaster.aitrade.data.SystemDB_TickersHistory;

public class Ticker_History {

	private static Ticker_History instance = new Ticker_History();

	public static Ticker_History getInstance() {
		return instance;
	}

	private static SystemDB_TickersHistory TICKERDB_HISTORY = SystemDB_TickersHistory.getInstance();

	private Queue<Ticker_Spot> ticker_Spots_Cache = new LinkedList<Ticker_Spot>();
	private Queue<TickerData> tickerDatas_Cache = new LinkedList<TickerData>();
	private int lastUnixTime = 0;
	private boolean hasNewData = true;

	public Ticker_Spot getTicker(String symbol) throws IOException {
		Ticker_Spot result = null;

		// 批量获取数据
		if (ticker_Spots_Cache.size() < 1000 && hasNewData) {
			lastUnixTime = TICKERDB_HISTORY.getTickersList(ticker_Spots_Cache, lastUnixTime);
			if (lastUnixTime == -1) {
				hasNewData = false;
			}
		}
		if (ticker_Spots_Cache.size() > 0) {
			result = ticker_Spots_Cache.poll();
		}

		return result;
	}

	public TickerData getTickerData(String symbol) throws IOException {
		TickerData result = null;

		// 批量获取数据
		if (ticker_Spots_Cache.size() < 1000 && hasNewData) {
			lastUnixTime = TICKERDB_HISTORY.getTickerDatasList(tickerDatas_Cache, lastUnixTime);
			if (lastUnixTime == -1) {
				hasNewData = false;
			}
		}
		if (tickerDatas_Cache.size() > 0) {
			result = tickerDatas_Cache.poll();
		}

		return result;
	}

}
